package dao;

import vo.Food;
import vo.Food;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class FoodD extends BaseD {

    public Food findWithId(String id) throws Exception{
        initConnection();
        Statement stat = conn.createStatement();
        String sql = "select * from food where id = '" + id + "'";
        ResultSet rs = stat.executeQuery(sql);
        Food stu = getFood(rs);
        closeConnection();
        return stu;
    }

    public ArrayList<Food> findWithName(String name) throws Exception{
        ArrayList<Food> al = new ArrayList<>();
        initConnection();
        Statement stat = conn.createStatement();
        String sql = "select * from food where name = '" + name + "'";
        ResultSet rs = stat.executeQuery(sql);
        getMoreFood(al, rs);
        closeConnection();
        return al;
    }

    public boolean insertFood(String id, String name, String desc, String img, String type, String date) throws Exception{
        initConnection();
        String sql = "insert into food(id, `name`, `desc`, `img`, `type`, `date`) values(?, ?, ?, ?, ?, ?)";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, id);
        ps.setString(2, name);
        ps.setString(3, desc);
        ps.setString(4, img);
        ps.setString(5, type);
        ps.setString(6, date);
        int i = ps.executeUpdate();
        closeConnection();
        return i == 1;
    }

    public boolean deleteFood(String id) throws Exception{

        initConnection();
        Statement stat = conn.createStatement();
        String sql = "delete from food where id='"+id+"'";
        int i = stat.executeUpdate(sql);
        closeConnection();
        return i==1;
    }

    public ArrayList<Food> getOnePage(int page, int size) throws Exception{
        ArrayList<Food> al = new ArrayList<>();
        initConnection();
        String sql = "SELECT * FROM food limit ?, ?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1, (page-1)*size);
        ps.setInt(2, size);
        ResultSet rs =  ps.executeQuery();
        getMoreFood(al, rs);
        closeConnection();
        return al;
    }

    public int getFoodCount() throws Exception{
        initConnection();
        String sql = "select count(*) from food";
        Statement stat = conn.createStatement();
        ResultSet rs = stat.executeQuery(sql);
        rs.next();
        int count = rs.getInt(1);
        closeConnection();
        return count;
    }

    public void updateFoodInfo(String id, String name, String desc, String img, String type, String date) throws Exception{

        initConnection();
        String sql = "update food set name=?, `desc`=?, `img`=?, `type`=?, `date`=? where id=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, name);
        ps.setString(2, desc);
        ps.setString(3, img);
        ps.setString(4, type);
        ps.setString(5, date);
        ps.setString(6, id);
        ps.executeUpdate();
        closeConnection();
    }

    private Food getFood(ResultSet rs) throws SQLException {
        Food food = null;
        if (rs.next()){
            food = new Food();
            food.setId(rs.getString("id"));
            food.setName(rs.getString("name"));
            food.setDesc(rs.getString("desc"));
            food.setImg(rs.getString("img"));
            food.setType(rs.getString("type"));
            food.setDate(rs.getString("date"));
        }
        return food;
    }

    private void getMoreFood(ArrayList<Food> al, ResultSet rs) throws SQLException {
        while (rs.next()){
            Food food = new Food();
            food.setId(rs.getString("id"));
            food.setName(rs.getString("name"));
            food.setDesc(rs.getString("desc"));
            food.setImg(rs.getString("img"));
            food.setType(rs.getString("type"));
            food.setDate(rs.getString("date"));
            al.add(food);
        }
    }


}
